package com.lzs.adx.web.modules.adx.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.lzs.adx.commmon.utils.Result;
import com.lzs.adx.web.commmon.utils.H5UrlReplacer;
import com.lzs.adx.web.commmon.utils.URLEncoderDecoderTool;
import com.lzs.adx.web.modules.adx.dto.ConfigLinkerDTO;
import com.lzs.adx.web.modules.adx.dto.TbConfigDTO;
import com.lzs.adx.web.modules.adx.dto.UserLinkerDTO;
import com.lzs.adx.web.modules.adx.service.ConfigLinkerService;
import com.lzs.adx.web.modules.adx.service.UserLinkerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;


@RestController
@Slf4j
@RequestMapping("/tb/kj")
public class KanJiaLinkController {


    @Resource
    ConfigLinkerService configLinkerService;

    @Resource
    private UserLinkerService userLinkerService;



    @GetMapping("/getConfig/{userId}")
    public Map getConfig(@PathVariable("userId") Long userId) {
        Map<String,Object> params=new HashMap<>();
        params.put("status",1L);
        params.put("userId",userId);
        List<ConfigLinkerDTO> list = configLinkerService.list(params);

        Map<String,Object> result=new HashMap<>();
        TbConfigDTO tbConfigDTO=new TbConfigDTO();
        if(CollectionUtil.isNotEmpty(list)){
            ConfigLinkerDTO configLinkerDTO = list.get(0);
            tbConfigDTO.setId(String.valueOf(configLinkerDTO.getId()));
            tbConfigDTO.setUrl(configLinkerDTO.getLinker());
            result.put("data",tbConfigDTO);
            log.info("已配置了上线的信息，ID为{}",tbConfigDTO.getId());
        }else{
            log.info("没有上线的信息");
            Map<String,Object> userlInker=new HashMap<>();
            userlInker.put("userId",userId);
            userlInker.put("type",1);
            List<UserLinkerDTO> list1 = userLinkerService.list(userlInker);

            UserLinkerDTO userLinkerDTO = list1.get(0);
            String h5Url = H5UrlReplacer.getH5Url(userLinkerDTO.getLinker());

            tbConfigDTO.setUrl(URLEncoderDecoderTool.decodeURL(h5Url));
            result.put("data",tbConfigDTO);
        }
        log.info("最终的信息为{}", JSON.toJSONString(result));

        return result;
    }


    /**
     * 上报
     * @param eventId
     * @return
     */
    @GetMapping("/event/report")
    @CrossOrigin(origins = "*") // 允许所有来源
    public Result report(String eventId) {

        ConfigLinkerDTO configLinkerDTO = configLinkerService.get(Long.valueOf(eventId));
        long total = Objects.isNull(configLinkerDTO.getConsumeNumber())?1L:configLinkerDTO.getConsumeNumber() + 1;

        //超过了就设置下架
        if(configLinkerDTO.getNumber()==total ||   total>configLinkerDTO.getNumber()){
            configLinkerDTO.setStatus(2L);
        }
        configLinkerDTO.setUpdateTime(new Date());
        configLinkerDTO.setConsumeNumber(total);
        configLinkerService.update(configLinkerDTO);

        return Result.ok();
    }

}
